package com.carsa.blackberry.ui.screen;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;

import com.carsa.blackberry.CarsaApp;
import com.carsa.blackberry.MainKeys;
import com.carsa.blackberry.domain.dto.EntregasQueueDTO;
import com.carsa.blackberry.domain.services.impl.EntregasServiceImpl;
import com.carsa.blackberry.ui.UIKeys;
import com.carsa.blackberry.ui.screen.common.BasicScreen;
import com.carsa.blackberry.ui.screen.entregasexitosas.EntregasExitosasScreen;
import com.carsa.blackberry.ui.screen.entregasfallidas.EntregasFallidasScreen;
import com.carsa.blackberry.ui.screen.gruposentregas.GruposEntregasScreen;
import com.carsa.blackberry.util.ui.UIUtil;

/**
 * @author Nahuel Barrios.
 * @since 20/07/2010.
 * @version 1.0
 */
public class MainMenuScreen extends BasicScreen {

    ButtonField bf_grupos_entregas;
    ButtonField bf_entregas_exitosas;
    ButtonField bf_entregas_fallidas;
    ButtonField bf_renovar_entregas;

    public MainMenuScreen() {
	super(UIKeys.TITLE_MENU_PPAL);
	super.customizeScreen();

	int fontSizeForMenu = 20;

	bf_grupos_entregas = new ButtonField(UIKeys.TITLE_GRUPOS_ENTREGAS, FIELD_HCENTER | ButtonField.USE_ALL_WIDTH) {
	    // public int getPreferredWidth() {
	    // return buttonsWidth;
	    // }
	};
	bf_grupos_entregas.setFont(UIUtil.getFontAtSize(this, fontSizeForMenu));
	bf_grupos_entregas.setChangeListener(new FieldChangeListener() {
	    public void fieldChanged(Field field, int context) {
		UiApplication.getUiApplication().popScreen(_currentScreen);
		UiApplication.getUiApplication().pushScreen(new GruposEntregasScreen(false));
	    }
	});

	bf_entregas_exitosas = new ButtonField(UIKeys.TITLE_ENTREGAS_EXITOSAS, FIELD_HCENTER
	        | ButtonField.USE_ALL_WIDTH) {
	    // public int getPreferredWidth() {
	    // return buttonsWidth;
	    // }
	};
	bf_entregas_exitosas.setFont(UIUtil.getFontAtSize(this, fontSizeForMenu));
	bf_entregas_exitosas.setChangeListener(new FieldChangeListener() {
	    public void fieldChanged(Field field, int context) {
		UiApplication.getUiApplication().popScreen(_currentScreen);
		UiApplication.getUiApplication().pushScreen(new EntregasExitosasScreen());
	    }
	});

	bf_entregas_fallidas = new ButtonField(UIKeys.TITLE_ENTREGAS_FALLIDAS, FIELD_HCENTER
	        | ButtonField.USE_ALL_WIDTH) {
	    // public int getPreferredWidth() {
	    // return buttonsWidth;
	    // }
	};
	bf_entregas_fallidas.setFont(UIUtil.getFontAtSize(this, fontSizeForMenu));
	bf_entregas_fallidas.setChangeListener(new FieldChangeListener() {
	    public void fieldChanged(Field field, int context) {
		UiApplication.getUiApplication().popScreen(_currentScreen);
		UiApplication.getUiApplication().pushScreen(new EntregasFallidasScreen());
	    }
	});

	bf_renovar_entregas = new ButtonField(UIKeys.TITLE_RENOVAR_ENTREGAS, FIELD_HCENTER | ButtonField.USE_ALL_WIDTH) {
	    // public int getPreferredWidth() {
	    // return buttonsWidth;
	    // }
	};
	bf_renovar_entregas.setFont(UIUtil.getFontAtSize(this, fontSizeForMenu));
	bf_renovar_entregas.setChangeListener(new FieldChangeListener() {
	    public void fieldChanged(Field field, int context) {
		String message = "";
		
		EntregasQueueDTO queue = (EntregasQueueDTO) CarsaApp.getMemory().get(MainKeys.PARAMETER_PENDING_QUEUE);
		if (queue.hasPendingEntregas((String)CarsaApp.getMemory().get(MainKeys.PARAMETER_LEGAJO_DRIVER)))
		    message = UIKeys.POPUP_QUEDAN_ENTREGAS_PENDIENTES;
		else {
		    int response= new EntregasServiceImpl().refreshEntregas();
		    if (response>0)
			message = UIKeys.POPUP_SE_RENOVARON_LAS_ENTREGAS_CORRECTAMENTE;
		    else if(response==0)
			message = UIKeys.POPUP_NO_HAY_ENTREGAS_NUEVAS;
		    else
			message = UIKeys.POPUP_NO_SE_RENOVARON_LAS_ENTREGAS;
		}
		Dialog.alert(message);
	    }
	});

	// ---------------------------------------------------
	// Con todos los controles ya seteados, armo la vista.
	_mainManager.add(new LabelField(UIKeys.FIELD_DRIVER + " " + CarsaApp.getMemory().get(MainKeys.PARAMETER_DRIVER_NAME), FIELD_RIGHT));

	_mainManager.add(new LabelField(UIKeys.FIELD_AYUDANTE + " " + CarsaApp.getMemory().get(MainKeys.PARAMETER_AYUDANTE_NAME), FIELD_RIGHT));

	_mainManager.add(UIUtil.getEmptyLineForResize(2));

	_mainManager.add(bf_grupos_entregas);
	_mainManager.add(bf_entregas_exitosas);
	_mainManager.add(bf_entregas_fallidas);
	_mainManager.add(bf_renovar_entregas);
	_mainManager.add(UIUtil.getEmptyLine());
	this.add(_mainManager);
    }

    public boolean onClose() {
	return false;
    }
    
}
